<!-- 代码生成弹框 -->
<template>
  <zero-dialog :showDialog.sync="fromVisible"
               :fullscreen.sync="fullscreen"
               :btnExist="btnExist"
               :loading="loading"
               :title="'代码生成'"
               :submit="generatorCode">
    <div slot="body">
      <el-form :model="from"
               :rules="rules"
               label-width="90px"
               ref="fromRef">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="databaseInfoId"
                          label="数据源">
              <el-select v-model="from.databaseInfoId"
                         filterable
                         clearable
                         placeholder="请选择数据源"
                         style="width: 100%"
                         @change="databaseChange">
                <el-option v-for="item in dataBases"
                           :key="item.key"
                           :label="item.value"
                           :value="item.key" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="catalog"
                          label="数据库名">
              <el-select v-model="from.catalog"
                         filterable
                         clearable
                         placeholder="请选择数据库名"
                         style="width: 100%"
                         @change="catalogChange">
                <el-option v-for="item in catalogs"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="tableInfo"
                          label="表信息">
              <el-select v-model="from.tableInfo"
                         filterable
                         clearable
                         multiple
                         placeholder="请选择表名称"
                         value-key="tableName"
                         style="width: 100%">
                <el-option v-for="item in tableNames"
                           :key="item.tableName"
                           :label="item.tableName"
                           :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="baseEntity"
                          label="实体基类">
              <el-select v-model="from.baseEntity"
                         placeholder="请选择实体基类"
                         style="width: 100%">
                <el-option key="BaseEntity"
                           label="BaseEntity"
                           value="BaseEntity"></el-option>
                <el-option key="BaseDataEntity"
                           label="BaseDataEntity"
                           value="BaseDataEntity"></el-option>
                <el-option key="BaseTreeEntity"
                           label="BaseTreeEntity"
                           value="BaseTreeEntity"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="12">
            <el-form-item prop="author"
                          label="作者">
              <el-input v-model.trim="from.author"
                        placeholder="请输入作者" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="packagePath"
                          label="包路径">
              <el-input v-model.trim="from.packagePath"
                        placeholder="请输入包路径" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="忽略的前缀">
              <el-input v-model.trim="from.tablePrefix"
                        placeholder="请输入忽略表前缀" />
            </el-form-item>
          </el-col>
          <span v-if="development">
            <el-col :span="12">
              <el-form-item label="是否下载">
                <el-radio-group v-model="from.download"
                                @change="downloadChange">
                  <el-radio-button label="T"
                                   key="T">是</el-radio-button>
                  <el-radio-button label="F"
                                   key="F">否</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </span>
        </el-row>
        <el-form-item v-if="existBasicPath"
                      prop="basicPath"
                      label="文件路径">
          <el-input v-model.trim="from.basicPath"
                    placeholder="请输入文件路径" />
        </el-form-item>
      </el-form>
    </div>
  </zero-dialog>
</template>

<script>
export default {
  name: 'GeneratorCodeFrom',
  props: {
    generatorCodeUrl: String
  },
  data () {
    return {
      loading: false,
      btnExist: true, // 按钮是否存在
      fullscreen: false, // 弹框全屏
      fromVisible: false, // 是否显示弹框
      from: {
        download: 'T',
        packagePath: 'com.zero.manage.web',
        basicPath: 'D:\\ideaProject\\zero-cloud\\manage\\manage-web\\src\\main\\java\\',
        author: 'zero'
      },
      dataBases: [], // 数据源
      catalogs: [], // 数据库
      tableNames: [], // 表
      databaseInfoId: '', // 选中后的数据源值
      existBasicPath: false, // 是否存在文件路径  -- 当下载的时候不需要文件路径，非下载的时候必填
      development: false, // 是否开发环境
      rules: {
        databaseInfoId: [{ required: true, message: '请选择数据源', trigger: 'change' }],
        catalog: [{ required: true, message: '请选择数据库', trigger: 'change' }],
        tableInfo: [{ required: true, message: '请选择表信息', trigger: 'change' }],
        baseEntity: [{ required: true, message: '请选实体基类', trigger: 'change' }],
        author: [{ required: true, message: '请输入作者', trigger: 'blur' }],
        packagePath: [{ required: true, message: '请输入包路径', trigger: 'blur' }],
        basicPath: [{ required: true, message: '请输入文件路径', trigger: 'blur' }]
      }
    }
  },
  methods: {
    initDialog () {
      this.development = process.env.NODE_ENV === 'development'
      this.initDatabase()
      this.fromVisible = true
      this.fromReset()
    },
    initDatabase () {
      // 数据源初始化
      this.$axios({ url: '/databaseInfo/select', method: 'get' }).then(res => {
        this.dataBases = res.data
      })
    },
    databaseChange (data) {
      if (data) {
        this.loading = true
        const config = {
          url: '/databaseInfo/getCatalogs',
          method: 'get',
          params: {
            databaseInfoId: data
          }
        }
        this.$axios(config).then(res => {
          this.catalogs = res.data
          this.loading = false
        }).catch(() => {
          this.from.databaseInfoId = ''
          this.loading = false
        })
        this.databaseInfoId = data
      }
    },
    catalogChange (data) {
      if (data) {
        this.loading = true
        const config = {
          url: '/databaseInfo/getTableInfo',
          method: 'get',
          params: {
            databaseInfoId: this.databaseInfoId,
            catalog: data
          }
        }
        this.$axios(config).then(res => {
          this.tableNames = res.data
          this.loading = false
        }).catch(() => {
          this.from.catalog = ''
          this.loading = false
        })
      }
    },
    downloadChange () {
      this.existBasicPath = this.from.download === 'F'
    },
    generatorCode () {
      // 添加或修改
      this.$refs.fromRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.btnExist = false
          const config = {
            url: this.generatorCodeUrl,
            method: 'post',
            data: this.from
          }
          if (this.from.download === 'T') {
            config.responseType = 'blob'
          }
          this.$axios(config)
            .then(res => {
              if (this.from.download === 'T') {
                // 这里res.data是返回的blob对象
                const blob = new Blob([res], {
                  type: 'application/zip;charset=UTF-8'
                })
                const downloadElement = document.createElement('a')
                const href = window.URL.createObjectURL(blob) // 创建下载的链接
                downloadElement.href = href
                downloadElement.download = 'generator.zip' // 下载后文件名
                document.body.appendChild(downloadElement)
                downloadElement.click() // 点击下载
                document.body.removeChild(downloadElement) // 下载完成移除元素
                window.URL.revokeObjectURL(href) // 释放掉blob对象
              }

              this.loading = false
              this.btnExist = true
              this.fromVisible = false
              this.$message.success('操作成功')
            })
            .catch(() => {
              this.loading = false
              this.btnExist = true
            })
        }
      })
    },
    fromReset () {
      // 重置表单
      this.$nextTick(() => {
        this.fullscreen = false
        this.$refs.fromRef.clearValidate()
      })
    }
  }
}
</script>
